package dk.simonvogensen.itodo.client.view;

import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import dk.simonvogensen.itodo.shared.model.Config;
import dk.simonvogensen.itodo.client.controller.Controller;
import dk.simonvogensen.itodo.shared.model.TodoItem;
import dk.simonvogensen.uirecorder.client.*;

import java.util.List;

public class ConfigDialogBox extends DialogBox {
    public ConfigDialogBox(final Controller controller) {
        super(false, false);

        Config config = controller.getConfig();

        setText("Config");
        VerticalPanel vPanel = new RecordableVerticalPanel();

        FlexTable table = new RecordableFlexTable();

        table.setWidget(0, 0, new RecordableLabel("project ids:"));
        final TextBox projectIdTextBox = new RecordableTextBox();
        projectIdTextBox.setText(config.getProjectIds());
        table.setWidget(0, 1, projectIdTextBox);

        table.setWidget(1, 0, new RecordableLabel("transport types:"));
        final TextBox transportTypesTextBox = new RecordableTextBox();
        transportTypesTextBox.setText(config.getTransportTypes());
        table.setWidget(1, 1, transportTypesTextBox);

        table.setWidget(2, 0, new RecordableLabel("employee id:"));
        final TextBox employeeIdTextBox = new RecordableTextBox();
        employeeIdTextBox.setText(config.getEmployeeId());
        table.setWidget(2, 1, employeeIdTextBox);

        table.setWidget(3, 0, new RecordableLabel("import/export:"));
        final TextArea exportTextArea = new RecordableTextArea();
        table.setWidget(3, 1, exportTextArea);

        vPanel.add(table);

        HorizontalPanel hPanel = new RecordableHorizontalPanel();

        final Button exportButton = new RecordableButton("Export", new ClickHandler() {
            public void onClick(ClickEvent event) {
                Button button = (Button) event.getSource();
                if(button.getText().equals("Export")) {
                    exportTextArea.setText(controller.exportData());
                } else {
                    controller.importData(exportTextArea.getText());
                }
            }
        });
        hPanel.add(exportButton);

        Button saveButton = new RecordableButton("Save", new ClickHandler() {
            public void onClick(ClickEvent event) {
                controller.getConfig().setProjectIds(projectIdTextBox.getText());
                controller.getConfig().setTransportTypes(transportTypesTextBox.getText());
                controller.getConfig().setEmployeeId(employeeIdTextBox.getText());
                controller.updateConfig();
                hide();
            }
        });
        hPanel.add(saveButton);

        Button closeButton = new RecordableButton("Close", new ClickHandler() {
            public void onClick(ClickEvent event) {
                hide();
            }
        });
        hPanel.add(closeButton);

        exportTextArea.addBlurHandler(new BlurHandler() {
            public void onBlur(BlurEvent event) {
                if(exportTextArea.getText().isEmpty()) {
                    exportButton.setText("Export");
                } else {
                    exportButton.setText("Import");
                }
            }
        });

        vPanel.add(hPanel);
        vPanel.setCellHorizontalAlignment(hPanel, HorizontalPanel.ALIGN_CENTER);
        setWidget(vPanel);
    }
}
